package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.UserGeoDAO;
import com.xinghou.platform.model.main.UserGeo;
import com.xinghou.platform.server.mq.MqMessage;

@Service
public class UserGeoDAOImpl  {/*
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private MqMessage mqMessage;
	
	@Override
	public UserGeo getUserGeoById(Integer geoId) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Geo_id,Longitude,Latitude,Geo_hash,Geo_long,Location,Create_time,Update_time FROM user_geo WHERE geo_id=?";
		UserGeo usergeo = jdbcTemplate.queryForObject(sql,new Object[] { geoId }, new UserGeoMapper());
		CustomerContextHolder.clearCustomerType();
		return usergeo;
	}

	@Override
	public List<UserGeo> getUserGeosById(int geoId, int pageSize) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Geo_id,Longitude,Latitude,Geo_hash,Geo_long,Location,Create_time,Update_time FROM user_geo WHERE geo_id > ? limit ?";
		List<UserGeo> usergeos = jdbcTemplate.query(sql,new Object[] { geoId, pageSize }, new UserGeoMapper());
		CustomerContextHolder.clearCustomerType();
		return usergeos;
	}

	@Override
	public int updateUserGeo(UserGeo usergeo) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"UPDATE user_geo SET Geo_id=?,Longitude=?,Latitude=?,Geo_hash=?,Geo_long=?,Location=?,Update_time=? WHERE Geo_id=?");
		Object[] params = { usergeo.getGeoId(),usergeo.getLongitude(),usergeo.getLatitude(),usergeo.getGeoHash(),usergeo.getGeoLong(),
				usergeo.getLocation(),usergeo.getUpdateTime(),usergeo.getGeoId()};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(), params);
		CustomerContextHolder.clearCustomerType();
		
		if(ret>0){
			mqMessage.sendMessageUsergeo(usergeo.getGeoId()+"",true);
		}
		return ret;
	}

	@Override
	public int saveUserGeo(UserGeo usergeo) throws DataAccessException {
		String sql = "INSERT INTO user_geo(Geo_id,Longitude,Latitude,Geo_hash,Geo_long,Location,Create_time,Update_time) VALUES (?,?,?,?,?,?,?,?)";
		Object[] params = {usergeo.getGeoId(),usergeo.getLongitude(),usergeo.getLatitude(),usergeo.getGeoHash(),
				usergeo.getGeoLong(),usergeo.getLocation(),usergeo.getCreateTime(),usergeo.getUpdateTime()};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.savenukey(sql.toString(),params);
		CustomerContextHolder.clearCustomerType();
		if(ret>0){
			mqMessage.sendMessageUsergeo(ret+"",false);
		}
		return ret;
	}

	@SuppressWarnings("unused")
	private static final class UserGeoMapper implements RowMapper<UserGeo> {
		public UserGeo mapRow(ResultSet rs, int rowNum) throws SQLException {
			UserGeo usergeo = new UserGeo();
			usergeo.setGeoId(rs.getInt("Geo_id"));
			usergeo.setLongitude(rs.getDouble("Longitude"));
			usergeo.setLatitude(rs.getDouble("Latitude"));
			usergeo.setGeoHash(rs.getString("Geo_hash"));
			usergeo.setGeoLong(rs.getLong("Geo_long"));
			usergeo.setLocation(rs.getString("Location"));
			usergeo.setCreateTime(rs.getTimestamp("Create_time"));
			usergeo.setUpdateTime(rs.getTimestamp("Update_time"));
			return usergeo;
			
		}
	}*/
}
